package com.victor.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.victor.entity.Answer;
import com.victor.entity.Label;
import com.victor.entity.Question;
import com.victor.entity.YhUsers;
import com.victor.service.QuestionService;
import com.victor.service.UserService;
import com.victor.utils.FileUpload;
import com.victor.utils.Msg;

/**
 * 后台提问
 * 
 * @author Mr.Zhang
 *
 */
@RestController
@RequestMapping("/question")
public class QuestionController {

	@Autowired
	private QuestionService questionService;
	@Autowired
	private UserService userService;

	// 查询提问列表(web)
	@RequestMapping("/getAll")
	public Msg getAllList(@RequestParam(value = "pn", defaultValue = "1") int pn) {
		PageMethod.startPage(pn, 8);
		List<Question> questions = questionService.getAllList();
		if (questions != null && questions.size() > 0) {
			for (Question question : questions) {
				question.setQ_user(userService.getUserByid(question.getUserid()));
			}
		}
		PageInfo page = new PageInfo(questions, 4);
		return Msg.success().add("questions", page);
	}

	// 搜索和日期筛选+标签查询（int lid）
	@RequestMapping("/getQuestionBySearch")
	public Msg getQuestionBySearch(@RequestParam(value = "pn", defaultValue = "1") int pn, String[] labels, String starttime, String endtime, String tiaojian) {
		PageMethod.startPage(pn, 8);

		List<Question> questions = questionService.getQuestionBySearch(starttime, endtime, tiaojian, labels);

		if (questions != null && questions.size() > 0) {
			for (Question question : questions) {
				question.setQ_user(userService.getUserByid(question.getUserid()));
			}
		}
		PageInfo page = new PageInfo(questions, 4);
		return Msg.success().add("questions", page);
	}

	// 通过id查询提问详情
	@RequestMapping("/findByid")
	public Msg findByid(int qid) {
		Question question = questionService.findByid(qid);
		if (question != null) {
			for (Answer answer : question.getAnswer()) {
				if (answer != null) {
					YhUsers yhuser = userService.getUserByid(answer.getA_userid());
					answer.setU_avatar(yhuser.getAvatar());
					answer.setU_nickname(yhuser.getNickname());
				}
			}
			return Msg.success().add("question", question);
		}
		return Msg.fail("未查询到该id的提问");
	}

	// 获取热门标签
	@RequestMapping("/getHotLabel")
	public Msg getHotLabel() {
		List<Label> labels = questionService.getAllLable();
		if (labels != null && labels.size() > 0) {
			if (labels.size() >= 8) {
				List<Label> labelret = labels.subList(0, 8);
				return Msg.success().add("hotLabel", labelret);
			}
			return Msg.success().add("hotLabel", labels);
		}
		return Msg.success().add("hotLabel", "[]");
	}

	// 修改
	@RequestMapping("/update")
	public Msg update(Question question, MultipartFile photofile, HttpServletRequest request) throws Exception {

		if (photofile != null) {
			String photo = FileUpload.uploadFile(photofile, request);
			question.setQ_filepath(photo);
		}
		boolean updResult = questionService.update(question);

		return updResult ? Msg.success() : Msg.fail();
	}

	// 批量删除
	@RequestMapping("/batchDelete")
	public Msg batchDelete(int[] ids) {
		boolean delResult = questionService.batchDelete(ids);

		return delResult ? Msg.success() : Msg.fail();
	}

}
